package ar.edu.taxi.persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class DAOUsers extends DataAccesObject implements IfDAO{

	@Override
	public int delete(String condicion) {
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "DELETE FROM users " +
						"WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}

	@Override
	public IfDTO find(String codigo) {
		DTOUsers user = null;
		Connection con = getConexion();
		if(codigo == null || codigo.length() == 0) return null;
		String query = "SELECT * FROM users " +
						"WHERE " + codigo;
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				user = new DTOUsers(rs.getInt("id"), rs.getString("name"), rs.getString("lastName"), 
						rs.getString("phone"), rs.getString("email"), rs.getString("password"), 
						rs.getInt("idUserType"), rs.getString("driverLicense"), rs.getString("userName"),
						rs.getBoolean("available"));
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public int insert(IfDTO bean) {
		int numFilas = 0;
		DTOUsers user = (DTOUsers) bean;
		Connection con = getConexion();
		String query = "INSERT INTO users VALUES( DEFAULT"+
						", '"+user.getName()+ "'" +
						", '"+user.getLastName()+ "'" +
						", '"+user.getPhone()+ "'" +
						", '"+user.getEmail()+ "'" +
						", '"+user.getPassword()+ "'" +
						", '"+user.getIdUserType()+ "'" +
						", '"+user.getDriverLicense()+ "'" +
						", '"+user.getUserName()+ "'" +
						", '"+(user.isAvailable()? 1:0)+ "'" +
						" )";
		try {
			Statement sentencia = con.createStatement();
			numFilas = sentencia.executeUpdate(query);
			sentencia.close();
			cerrarConexion( con );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return numFilas;
	}

	@Override
	public Vector<IfDTO> select(String condicion) {
		Vector<IfDTO> users = new Vector<IfDTO>();
		Connection con = getConexion();
		String query = "SELECT * FROM users " +
						(condicion == null || condicion.length() == 0 ? "" : "WHERE " + condicion) +
						" ORDER BY id";
	    try {
			Statement sentencia = con.createStatement();
			ResultSet rs = sentencia.executeQuery( query );
			while( rs.next() ){
				DTOUsers user = new DTOUsers(rs.getInt("id"), rs.getString("name"), rs.getString("lastName"), 
						rs.getString("phone"), rs.getString("email"), rs.getString("password"), 
						rs.getInt("idUserType"), rs.getString("driverLicense"), rs.getString("userName"),
						rs.getBoolean("available"));
				users.add(user);
			}
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return users;
	}

	@Override
	public int update(IfDTO dto, String condicion) {
		DTOUsers user = (DTOUsers) dto;
		int updated = 0;
		Connection con = getConexion();
		if(condicion == null || condicion.length() == 0) return 0;
		String query = "UPDATE users SET" +
						" name='" + user.getName() + "'" +
						", lastName='" + user.getLastName() + "'" +
						", phone='" + user.getPhone() + "'" +
						", email='" + user.getEmail() + "'" +
						", password='" + user.getPassword() + "'" +
						", idUserType='" + user.getIdUserType() + "'" +
						", driverLicense='" + user.getDriverLicense() + "'" +
						", userName='" + user.getUserName() + "'" +
						", available='" + (user.isAvailable()? 1:0) + "'" +
						" WHERE " + condicion;
	    try {
			Statement sentencia = con.createStatement();
			updated = sentencia.executeUpdate( query );
			sentencia.close();
			cerrarConexion(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}

}
